<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
      <uni-forms-item name="category_id" label="">
        <uni-easyinput placeholder="分类 id，参考`opendb-mall-categories`表" v-model="formData.category_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="jh_time" label="交货期">
        <uni-easyinput placeholder="交货期" v-model="formData.jh_time"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="goods_sn" label="货品编码" required>
        <uni-easyinput placeholder="商品的唯一货品编码" v-model="formData.goods_sn" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="purchaser" label="采购人员">
        <uni-easyinput placeholder="采购员" v-model="formData.purchaser" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="sale_num" label="销售数量">
        <uni-easyinput placeholder="销售数量" type="number" v-model="formData.sale_num"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="sale_price" label="销售金额">
        <uni-easyinput placeholder="销售金额" type="number" v-model="formData.sale_price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="price" label="成本单价" required>
        <uni-easyinput placeholder="成本单价" type="number" v-model="formData.price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="shop_price" label="成本单价">
        <uni-easyinput placeholder="成本单价" type="number" v-model="formData.shop_price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="all_price" label="成本总价">
        <uni-easyinput placeholder="成本总价" type="number" v-model="formData.all_price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="priceRecord" label="单价记录">
        <uni-data-checkbox :multiple="true" v-model="formData.priceRecord"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="total_price" label="总销售价格">
        <uni-easyinput placeholder="总销售价格" type="number" v-model="formData.total_price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="name" label="货品名称" required>
        <uni-easyinput placeholder="货品名称" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="position" label="货品位置">
        <uni-easyinput placeholder="货品位置" v-model="formData.position" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="type" label="规格型号" required>
        <uni-easyinput placeholder="规格型号" v-model="formData.type" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="code" label="物料编码">
        <uni-easyinput placeholder="物料编码" v-model="formData.code" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="danwei" label="计量单位">
        <uni-easyinput placeholder="计量单位" v-model="formData.danwei" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="unit_id" label="供货商">
        <select placeholder="供货商" v-model="formData.unit_id" trim="both"></select>
      </uni-forms-item>
      <uni-forms-item name="keywords" label="关键字">
        <uni-easyinput placeholder="商品关键字，为搜索引擎收录使用" v-model="formData.keywords" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="goods_desc" label="产品中文描述">
        <uni-easyinput placeholder="产品中文描述" v-model="formData.goods_desc" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="moq" label="最少清洗量">
        <uni-easyinput placeholder="MOQ " type="number" v-model="formData.moq"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="qx_type" label="清洗方式">
        <uni-easyinput placeholder="清洗方式" v-model="formData.qx_type" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="qx_price" label="清洗价格">
        <uni-easyinput placeholder="清洗价格" type="number" v-model="formData.qx_price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="goods_thumb" label="缩略图地址">
        <uni-easyinput placeholder="商品缩略图，用于在列表或搜索结果中预览显示" v-model="formData.goods_thumb" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="goods_fj" label="附件地址">
        <uni-data-checkbox :multiple="true" v-model="formData.goods_fj"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="remain_count" label="库存数量" required>
        <uni-easyinput placeholder="库存数量" type="number" v-model="formData.remain_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="zm_count" label="帐面库存">
        <uni-easyinput placeholder="帐面库存" type="number" v-model="formData.zm_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="sj_count" label="实际库存">
        <uni-easyinput placeholder="实际库存" type="number" v-model="formData.sj_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="wqx_count" label="未清洗库存">
        <uni-easyinput placeholder="未清洗" type="number" v-model="formData.wqx_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="yqx_count" label="已清洗">
        <uni-easyinput placeholder="已清洗" type="number" v-model="formData.yqx_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="month_sell_count" label="月销量">
        <uni-easyinput placeholder="月销量" type="number" v-model="formData.month_sell_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="total_sell_count" label="总销量">
        <uni-easyinput placeholder="总销量" type="number" v-model="formData.total_sell_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="add_date" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.add_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="last_modify_date" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.last_modify_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="seller_note" label="">
        <uni-easyinput placeholder="商家备注，仅商家可见" v-model="formData.seller_note" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/opendb-mall-goods.js';

  const db = uniCloud.database();
  const dbCollectionName = 'opendb-mall-goods';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.indexOf(key) > -1) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "category_id": "",
        "jh_time": "",
        "goods_sn": "",
        "purchaser": "",
        "sale_num": null,
        "sale_price": null,
        "price": null,
        "shop_price": null,
        "all_price": null,
        "priceRecord": [],
        "total_price": null,
        "name": "",
        "position": "",
        "type": "",
        "code": "",
        "danwei": "",
        "unit_id": "",
        "keywords": "",
        "goods_desc": "",
        "moq": null,
        "qx_type": "",
        "qx_price": null,
        "goods_thumb": "",
        "goods_fj": [],
        "remain_count": null,
        "zm_count": null,
        "sj_count": null,
        "wqx_count": null,
        "yqx_count": null,
        "month_sell_count": null,
        "total_sell_count": null,
        "add_date": null,
        "last_modify_date": null,
        "seller_note": ""
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("category_id,jh_time,goods_sn,purchaser,sale_num,sale_price,price,shop_price,all_price,priceRecord,total_price,name,position,type,code,danwei,unit_id,keywords,goods_desc,moq,qx_type,qx_price,goods_thumb,goods_fj,remain_count,zm_count,sj_count,wqx_count,yqx_count,month_sell_count,total_sell_count,add_date,last_modify_date,seller_note").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>

<style>
  .uni-container {
    padding: 15px;
  }

  .uni-input-border,
  .uni-textarea-border {
    width: 100%;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    box-sizing: border-box;
  }

  .uni-input-border {
    padding: 0 10px;
    height: 35px;

  }

  .uni-textarea-border {
    padding: 10px;
    height: 80px;
  }

  .uni-button-group {
    margin-top: 50px;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    justify-content: center;
  }

  .uni-button {
    width: 184px;
  }
</style>
